package Controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.sql.SQLException;

import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;

import Dao.ViewRequestDao;
import Entity.Transfer;
import Model.ViewRequestModel;
import View.ViewRequestView;

public class ViewRequestController {
	private ViewRequestView view;
	private ViewRequestModel model;
	ViewRequestDao dao ;
	private JInternalFrame frame;
	
	public ViewRequestController(){
		frame = new JInternalFrame();
		view = new ViewRequestView();
		model = new ViewRequestModel();
		view.setModel(model);
		doAction();
		frame.setDefaultCloseOperation(JInternalFrame.EXIT_ON_CLOSE);
		frame.add(view);
		frame.setVisible(true);
		frame.setSize(800, 500);
		//System.out.println(LoginController.name);
	}

	private void doAction(){
		// TODO Auto-generated method stub
		doTransfer();
		loadTable();
		doSelectTable();
		doExit();
	}

	private void doTransfer() {
		// TODO Auto-generated method stub
		try {
			model.transfer();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void doSelectTable() {
		// TODO Auto-generated method stub
		view.doSelectTable(new KeyAdapter() {

			public void keyReleased(KeyEvent e) {
				doSelect();
			}
		});

		view.doSelectTable(new MouseAdapter() {

			public void mouseClicked(MouseEvent e) {
				doSelect();
			}
		});
	}
	public JInternalFrame getFrame(){
		return frame;
	}

	private void doSelect(){
		int row = view.getSelectedItem();
		dao = new ViewRequestDao();
//		Transfer transfer = new Transfer(Integer.parseInt(view.getValueAt(row, 0).toString()), view.getValueAt(row, 1).toString(), view.getValueAt(row, 2).toString(), view.getValueAt(row, 3).toString(), view.getValueAt(row, 4).toString(), view.getValueAt(row, 5).toString());
		Transfer transfer;
		try {
			transfer = dao.selectTableLoad(Integer.parseInt(view.getValueAt(row, 0).toString()));
			view.setName(transfer.getEmpName());
			view.setNoTranfer(""+transfer.getID());
			view.setNumber(transfer.getEmpNumber());
			view.setToLocation(transfer.getToLocation());
			view.setFromLocation(transfer.getFromLocation());
			view.setToProject(transfer.getToProject());
			view.setFromProject(transfer.getFromProject());
			view.setToDept(transfer.getToDepartment());
			view.setFromDept(transfer.getFromDepartment());
			view.enable();
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private void doExit() {
		// TODO Auto-generated method stub
		view.doExit(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				frame.dispose();
			}
		});
	}

	private void loadTable(){
		// TODO Auto-generated method stub
		try {
			model.transfer();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		view.setModel(model);
	}
	public static void main(String[] agrs) throws IOException, ClassNotFoundException, SQLException {
		new ViewRequestController();
	}
}
